package com.wj.rpsm.user.controller;

import com.wj.rpsm.user.pojo.Attention;
import com.wj.rpsm.user.pojo.Comment;
import com.wj.rpsm.user.pojo.Message;
import com.wj.rpsm.user.pojo.vo.ResponsePojo;
import com.wj.rpsm.user.pojo.vo.CommentVo;
import com.wj.rpsm.user.pojo.vo.ForumVo;
import com.wj.rpsm.user.service.AttentionService;
import com.wj.rpsm.user.service.CommentService;
import com.wj.rpsm.user.service.MessageService;
import com.wj.rpsm.user.utils.DateUtil;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;
import java.util.Optional;

/**
 * @author WeiJin
 * @version 1.0
 */
@RestController
@Api(tags = "论坛相关api")
@RequestMapping("/forum")
public class ForumController {

    @Autowired
    private MessageService messageService;
    @Autowired
    private AttentionService attentionService;
    @Autowired
    private CommentService commentService;


    /**
     * 查看帖子可根据标题获取，按浏览量和点赞量排序，论坛首页展示
     *
     * @return
     */
    @ApiOperation(value = "查看帖子,可根据标题、用户id、帖子id", notes = "按浏览量和点赞量排序，论坛首页展示")
    @GetMapping("/queryMessage")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "title", dataType = "String"),
            @ApiImplicitParam(name = "messageId", dataType = "Integer"),
            @ApiImplicitParam(name = "id", dataType = "Integer"),
            @ApiImplicitParam(name = "fromId", dataType = "Integer")

    })
    public ResponsePojo<List<ForumVo>> queryAllMessage(
            @RequestParam(value = "title", required = false) String title,
            @RequestParam(value = "messageId", required = false) Integer messageId,
            @RequestParam(value = "id", required = false) Integer id,
            @RequestParam(value = "fromId", required = false) Integer fromId) {
        return ResponsePojo.success(messageService.queryMessage(id, messageId, title, fromId));
    }

    @ApiOperation(value = "新增浏览量")
    @PutMapping("/updateBrowse/{id}")
    @ApiImplicitParam(name = "id", dataType = "Integer", required = true)
    public ResponsePojo<Integer> updateBrowse(@PathVariable("id") Integer id) {
        return ResponsePojo.success(messageService.updateMessageBrowse(id));
    }


    /**
     * 根据用户id获取关注列表
     *
     * @param id
     * @return
     */
    @ApiOperation("根据用户id查询关注列表")
    @ApiImplicitParam(name = "id", required = true, dataType = "Integer", paramType = "path")
    @GetMapping("/queryAttentionByUserId/{id}")
    public ResponsePojo<List<ForumVo>> queryAttentionByUserId(@PathVariable("id") Integer id) {
        return ResponsePojo.success(attentionService.queryAttentionByUserId(id));
    }

    /**
     * 根据帖子id获取所有评论
     *
     * @param messageId
     * @return
     */
    @ApiOperation("根据帖子id获取所有评论")
    @ApiImplicitParam(name = "messageId", required = true, dataType = "Integer", paramType = "path")
    @GetMapping("/queryComment/{messageId}")
    public ResponsePojo<List<CommentVo>> queryCommentByMessageId(@PathVariable("messageId") Integer messageId) {
        List<CommentVo> comments = commentService.queryCommentByMessageId(messageId);
        if (comments.size() <= 0) {
            return ResponsePojo.fail(comments, "该贴没有评论");
        }
        return ResponsePojo.success(comments, comments.size());
    }


    /**
     * 新建帖子
     *
     * @return
     */
    @PostMapping(value = "/addMessage")
    @ApiOperation(value = "新建帖子", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponsePojo<Integer> addMessage(@RequestBody Message message) {
        message.setMessage_publish_time(DateUtil.getNowTime());
        return ResponsePojo.success(messageService.addMessage(message));
    }

    /**
     * 新增回复
     *
     * @param comment
     * @return
     */
    @ApiOperation("新增回复")
    @PostMapping("/addComment")
    public ResponsePojo<Integer> addComment(@RequestBody Comment comment) {

        comment.setComment_publish_time(DateUtil.getNowTime());
        return ResponsePojo.success(commentService.addComment(comment));
    }

    /**
     * 帖子点赞
     *
     * @param messageId
     * @return
     */
    @ApiOperation("帖子点赞")
    @PutMapping("/messageLike/{messageId}")
    public ResponsePojo<Integer> updateMessageLike(@PathVariable("messageId") Integer messageId) {

        Integer result = messageService.updateMessageLike(messageId);
        if (result == 0) {
            return ResponsePojo.fail(result, "该id不存在");
        } else if (result < 0) {
            return ResponsePojo.fail(result, "出错了，请联系管理员");
        }
        return ResponsePojo.success(result);
    }

    /**
     * 帖子取消赞
     *
     * @param messageId
     * @return
     */
    @ApiOperation("帖子取消赞")
    @PutMapping("/messageUnlike/{messageId}")
    public ResponsePojo<Integer> updateMessageUnlike(@PathVariable("messageId") Integer messageId) {

        Integer result = messageService.updateMessageUnlike(messageId);
        if (result == 0) {
            return ResponsePojo.fail(result, "该id不存在");
        } else if (result < 0) {
            return ResponsePojo.fail(result, "出错了，请联系管理员");
        }
        return ResponsePojo.success(result);
    }


    /**
     * 评论点赞
     *
     * @param commentId
     * @return
     */
    @PutMapping("/commentLike/{commentId}")
    @ApiOperation("评论点赞")
    public ResponsePojo<Integer> updateCommentLike(@PathVariable("commentId") Integer commentId) {
        Integer result = commentService.updateMessageLike(commentId);
        if (result == 0) {
            return ResponsePojo.fail(result, "该id不存在");
        } else if (result < 0) {
            return ResponsePojo.fail(result, "出错了，请联系管理员");
        }
        return ResponsePojo.success(result);
    }

    /**
     * 评论取消点赞
     *
     * @param commentId
     * @return
     */
    @PutMapping("/commentUnlike/{commentId}")
    @ApiOperation("评论取消点赞")
    public ResponsePojo<Integer> updateCommentUnlike(@PathVariable("commentId") Integer commentId) {
        Integer result = commentService.updateMessageLike(commentId);
        if (result == 0) {
            return ResponsePojo.fail(result, "该id不存在");
        } else if (result < 0) {
            return ResponsePojo.fail(result, "出错了，请联系管理员");
        }
        return ResponsePojo.success(result);
    }

    @PostMapping("/addAttention")
    @ApiOperation("添加关注")
    public ResponsePojo<Integer> addAttention(@RequestBody Attention attention) {
        Attention a = attentionService.queryIsAttend(attention);
        attention.setAttend_date(LocalDate.now());
        if (Optional.ofNullable(a).isPresent()) {
            attention.setIsAttend(1);
            ResponsePojo.success(attentionService.updateIsAttend(attention));
        }

        return ResponsePojo.success(attentionService.addAttention(attention));
    }

    /**
     * 取消关注
     *
     * @param attention
     * @return
     */
    @DeleteMapping("/cancelAttention")
    @ApiOperation("取消关注")
    public ResponsePojo<Integer> cancelAttention(@RequestBody Attention attention) {
        attention.setAttend_date(DateUtil.getNowDate());
        return ResponsePojo.success(attentionService.cancelAttention(attention));
    }


}
